package cn.edu.ctbu.api01.restcontroller;


import cn.edu.ctbu.api01.domain.Student;
import cn.edu.ctbu.api01.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/webapi/student")
public class StudentRestController {

    @Autowired
    private StudentService studentService;

    /**
     * 读取的url:/webapi/student/list
     * @return
     */

    @GetMapping("/list")

    public List<Student> findAll(){
        List<Student> students=studentService.findAll();
        return students;
    }

    /**
     * 读取的url:/webapi/student/get/id
     * @return
     */

    @GetMapping("/get/{id}")

    public Student get(@PathVariable Long id){
        Student student=studentService.getById(id);
        student.setPassword("");
        return student;
    }


    /**
     * 新增学生的方法，/webapi/student/insert
     * @param student
     * @return
     */
    @PostMapping("/insert")
    public Student insert(Student student){

        Student student1=studentService.insert(student);

        return student1;
    }

    /**
     * 更新学生的方法，/webapi/student/update
     * @param student
     * @return
     */
    @PutMapping("/update")
    public Student update(Student student){

        Student student1=studentService.update(student);

        return student1;
    }

    /**删除学生的方法：/webapi/student/delete
     * @param id
     */
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Long id){
        Student student = studentService.findById(Math.toIntExact(id));
        if(student != null){
            studentService.delete(student);
        }
    }


}
